<template>
  <div class="login_container">
    <div class="login_box">
      <div class="img_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form ref="loginFormRef" class="login_form" :model="loginForm" :rules="LoginRules">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-yonghuming"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-mima"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login()">登录</el-button>
          <el-button type="info" @click="reset()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      //这是表单的登陆规则
      LoginRules: {
        //验证用户名
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输如密码", trigger: "blur" },
         // { min: 6, max: 10, message: "长度在 6个字符以上", trigger: "blur" },
        ],
      },
    };
  },
  methods:{
      reset(){
          //this.$refs.loginFormRef 代表表单对象
         this.$refs.loginFormRef.resetFields()
      },
      login(){
          this.$refs.loginFormRef.validate(val => {
              if(!val) return;
              this.$http.get('http://localhost:8088/login').then(res => {

                  //假装判断 username == 'admin' passsword = '123456'
                  let username = (this.loginForm.username == 'admin')
                  let password = (this.loginForm.password == '123456') 
                  if(username && password){
                       this.$message.success('success')
                       window.sessionStorage.setItem('token','2342386387567834')
                       this.$router.push('/home')
                  }else{
                        
                        this.$message.error('error')
                  }
              })  

          })
      }
  }
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .img_box {
    width: 130px;
    height: 130px;
    border-width: 20px;
    border-radius: 50%;
    position: absolute;
    border: solid 10px #fff;
    // height: 100px;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #eee;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
}

.login_form {
  width: 100%;
  padding: 0 20px;
  bottom: 0;
  position: absolute;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>>
